/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <ace/xcomponent/native_interface_xcomponent.h>
#include "napi/native_api.h"
#include <bits/alltypes.h>
#include <string>
#include "pag/pag.h"
#include <native_vsync/native_vsync.h>
#include "PagSurface.h"
#include "PagPlayerProxy.h"

namespace NativeXComponentSample {
void SetupSurface(OH_NativeXComponent *component, void *nativeWindow, void *eglContext);
// extern pag::PAGPlayer *m_Pagplayer;
class PAGPlayer {
public:
    explicit PAGPlayer(napi_env pEnv) { pagPlayerNapiProxy_ = new PagPlayerProxy(); };
    PAGPlayer(napi_env pEnv, std::string &id);
    PAGPlayer(napi_env pEnv, napi_ref pRef);
    ~PAGPlayer();
    static PAGPlayer *getInstance(napi_env pEnv, std::string &id);
    static napi_value PagInit(napi_env env, napi_callback_info info);
    static napi_value Prepare(napi_env env, napi_callback_info info);
    static napi_value SetComposition(napi_env env, napi_callback_info info);
    static napi_value Duration(napi_env env, napi_callback_info info);
    static napi_value CurrentFrame(napi_env env, napi_callback_info info);
    static napi_value GetProgress(napi_env env, napi_callback_info info);
    static napi_value JsPlayerConstructor(napi_env env, napi_callback_info info);
    static void Destructor(napi_env env, void *nativeObject, void *finalize); // 释放资源的函数(类似类的析构函数)
    static napi_value SetSurface(napi_env env, napi_callback_info info);
    static napi_ref sJSPlayerConstructor_; // 生命周期变量
    static std::string _msg;               // 设置和获取数据的变量
    napi_env mEnv = nullptr;               // 记录环境变量
    napi_ref mRef = nullptr;               // 记录生命周期变量

public:
    //注册回调
    typedef void (*FuncReg)(napi_value total, napi_value cur);
    static void RegFrameListener(FuncReg actFrame);

    static napi_value GetState(napi_env env, napi_callback_info info);
    //暂停
    static napi_value Pause(napi_env env, napi_callback_info info);
    //恢复播放
    static napi_value Resume(napi_env env, napi_callback_info info);
    //停止播放
    static napi_value Stop(napi_env env, napi_callback_info info);
    //添加动画播放状态监听
    static napi_value AddPlayerListener(napi_env env, napi_callback_info info);

    static napi_value SetRepeatCount(napi_env env, napi_callback_info info);

    static napi_value SetProgress(napi_env env, napi_callback_info info);

    static std::string GetCurrentXCompentId(napi_env env, napi_callback_info info);
    // 缩放与拉伸
    static napi_value SetScaleMode(napi_env env, napi_callback_info info);
    // 返回当前缩放模式
    static napi_value ScaleMode(napi_env env, napi_callback_info info);

    static PAGPlayer *GetNapiPlayer(napi_env env, napi_callback_info info);

    static napi_value NativeSetMatrix(napi_env env, napi_callback_info info);

    // 设置cacheEnabled属性的值
    static napi_value SetCacheEnabled(napi_env env, napi_callback_info info);
    // 设置cacheScale属性的值
    static napi_value SetCacheScale(napi_env env, napi_callback_info info);
    // 设置渲染的max帧速率
    static napi_value SetMaxFrameRate(napi_env env, napi_callback_info info);
    // 设置useDiskCache属性的值
    static napi_value SetUseDiskCache(napi_env env, napi_callback_info info);
    // 设置videoEnabled属性的值
    static napi_value SetVideoEnabled(napi_env env, napi_callback_info info);
    // 如果设置为true，PAG将把相关的渲染数据缓存到磁盘文件中，例如视频合成的解码图像帧
    static napi_value UseDiskCache(napi_env env, napi_callback_info info);
    // 如果设置为false，PAGView将跳过视频合成的渲染
    static napi_value VideoEnabled(napi_env env, napi_callback_info info);
    // 渲染的max帧速率
    static napi_value MaxFrameRate(napi_env env, napi_callback_info info);
    // 如果设置为true，PAG渲染器将缓存每个层的静态内容的内部位图表示
    static napi_value CacheEnabled(napi_env env, napi_callback_info info);
    // 该值定义内部图形缓存的比例因子，范围从0.0到1.0
    static napi_value CacheScale(napi_env env, napi_callback_info info);
    static napi_value GetComposition(napi_env env, napi_callback_info info);
    static napi_value GetLayersUnderPoint(napi_env env, napi_callback_info info);
    // 调用此方法可立即渲染当前位置
    static napi_value FlushAndFenceSync(napi_env env, napi_callback_info info);

    static napi_value RegisterRepeatCountCallback(napi_env env, napi_callback_info info);
    static napi_value RegisterCurrentFrameCallback(napi_env env, napi_callback_info info);
    static napi_value RemovePlayStateListener(napi_env env, napi_callback_info info);
    static napi_value RemoveRepeatCountListener(napi_env env, napi_callback_info info);
    static napi_value RemoveCurrentFrameListener(napi_env env, napi_callback_info info);
    static napi_value FreeCache(napi_env env, napi_callback_info info);

private : 
    static std::unordered_map<std::string, PAGPlayer *> pagPlayerNapi_;
    PagPlayerProxy *pagPlayerNapiProxy_;
};
}